# pluginDescription
# @name	Popup Notification
# @author Pau Tomas
# @description Alerts of new mails and events with a popupbox
# /pluginDescription

import pygtk, gtk, gobject

class popupnotification:

    def newMessagesReceived(self, data = [], fullCount = 0):
        print 'Entering popupnotification';
        self.data = data
        if (len(self.data) > 0):            
            self.window = gtk.Window(gtk.WINDOW_POPUP)
            self.window.set_decorated(False)
            self.window.set_property("skip-taskbar-hint", True)
            self.window.set_default_size(250, 150)

            vbox = gtk.VBox(False, 0)
            vbox.set_border_width(0)
            self.window.add(vbox)
            vbox.show()

            hbox = gtk.HBox(False, 0)
            hbox.set_border_width(0)
            hbox.show()
            vbox.pack_start(hbox, True, False, 0)

            prevButton = gtk.Button('<')
            prevButton.connect("clicked", lambda w: self.prevData())
            prevButton.set_flags(gtk.CAN_DEFAULT)
            prevButton.show()
            prevButton.set_size_request(25, 25)
            hbox.pack_start(prevButton, True, False, 0)

            nextButton = gtk.Button('>')
            nextButton.connect("clicked", lambda w: self.nextData())
            nextButton.set_flags(gtk.CAN_DEFAULT)
            nextButton.show()
            nextButton.set_size_request(25, 25)
            hbox.pack_start(nextButton, True, False, 0)

            closeButton = gtk.Button('X')
            closeButton.connect("clicked", lambda w: self.closePopup())
            closeButton.set_flags(gtk.CAN_DEFAULT)
            closeButton.show()
            closeButton.set_size_request(25, 25)
            hbox.pack_start(closeButton, True, False, 0)

            label = gtk.Label('%i new mail!' % fullCount)
            vbox.pack_start(label, True, False, 0)
            label.show()

            self.content_label = gtk.Label('')
            vbox.pack_start(self.content_label, True, False, 0)
            self.content_label.show()

            self.textbuffer = gtk.TextBuffer()
            self.textbuffer.set_text('')
            self.textview = gtk.TextView(self.textbuffer)
            self.textview.set_wrap_mode(gtk.WRAP_WORD)
            vbox.pack_start(self.textview, True, False, 0)
            self.textview.show()

            width, height = self.window.get_size()
            self.window.set_gravity(gtk.gdk.GRAVITY_SOUTH_EAST)
            self.window.move(gtk.gdk.screen_width() - width, gtk.gdk.screen_height() - height)

            self.data_counter = 0
            self.content_label.set_label(self.data[self.data_counter]['title'])
            self.textbuffer.set_text(self.data[self.data_counter]['summary'])

            self.popuptimer = gobject.timeout_add(2000, self.nextData)
            self.window.show()
        return None    	

    def prevData(self):
        if (self.data_counter - 1 >= 0):
            self.data_counter -= 1        
            self.content_label.set_label(self.data[self.data_counter]['title'][:30])
            self.textbuffer.set_text(self.data[self.data_counter]['summary'])            

    def nextData(self):
        if (self.data_counter + 1 < len(self.data)):
            self.data_counter += 1
            self.content_label.set_label(self.data[self.data_counter]['title'][:30])
            self.textbuffer.set_text(self.data[self.data_counter]['summary'])
            print 'nextData'
            return True
        else:
            self.closePopup()
            return False

    def closePopup(self):
        print "Close Popup"
        self.window.destroy()    

    def newEventReminder(self, data = []):
        print 'Entering Event Popupnotification';
        self.data = data
        if (len(self.data) > 0):            
            self.window = gtk.Window(gtk.WINDOW_POPUP)
            self.window.set_decorated(False)
            self.window.set_property("skip-taskbar-hint", True)
            self.window.set_default_size(250, 150)

            vbox = gtk.VBox(False, 0)
            vbox.set_border_width(0)
            self.window.add(vbox)
            vbox.show()

            self.content_label = gtk.Label('')
            vbox.pack_start(self.content_label, True, False, 0)
            self.content_label.show()

            self.textbuffer = gtk.TextBuffer()
            self.textbuffer.set_text('')
            self.textview = gtk.TextView(self.textbuffer)
            self.textview.set_wrap_mode(gtk.WRAP_WORD)
            vbox.pack_start(self.textview, True, False, 0)
            self.textview.show()

            width, height = self.window.get_size()
            self.window.set_gravity(gtk.gdk.GRAVITY_NORTH_EAST)
            self.window.move(gtk.gdk.screen_width() - width, 30)

            self.content_label.set_label(self.data[0]['title'])

            self.popuptimer = gobject.timeout_add(2000, self.nextData)
            self.window.show()
        return None    	
        

